package view;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Insets;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

import buscador.Buscador;


public class TelaPrincipal {

	private JFrame frmBuscador;
	private JTextField textFieldConsulta;
	private JTextArea textAreaResultados;
//	private JComboBox comboBoxConsulta;

	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					//new LeituraQuery().lerQuerys();
					//Indexador.getInstance().indexarBase();
					TelaPrincipal window = new TelaPrincipal();
					window.frmBuscador.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
    
	/**
	 * Create the application.
	 */
	public TelaPrincipal() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmBuscador = new JFrame();
		frmBuscador.setResizable(false);
		frmBuscador.setMinimumSize(new Dimension(600, 350));
		frmBuscador.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 12));
		frmBuscador.setTitle("Buscador");
		frmBuscador.getContentPane().setForeground(SystemColor.activeCaption);
		frmBuscador.getContentPane().setBackground(SystemColor.activeCaption);
		
		JScrollPane scrollPane = new JScrollPane();
		textAreaResultados = new JTextArea();
		textAreaResultados.setMaximumSize(new Dimension(550, 400));
		textAreaResultados.setMargin(new Insets(3, 3, 3, 3));
		textAreaResultados.setAlignmentY(10.0f);
		textAreaResultados.setAlignmentX(10.0f);
		scrollPane.setViewportView(textAreaResultados);
		
		JLabel lblConsulta = new JLabel("Consulta:");
		lblConsulta.setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		Vector<String> vetor = new Vector<String>();
		for (Integer i = 1 ; i <= 100; i++) {
			vetor.add(i.toString());
		}
		
		//comboBoxConsulta = new JComboBox(vetor);
		textFieldConsulta = new JTextField();
//		textFieldConsulta.setVisible(false);
//		textFieldConsulta.setEditable(false);
//		textFieldConsulta.setEnabled(false);
		textFieldConsulta.setColumns(10);
				
		JButton btnPesquisar = new JButton("Pesquisar");
		btnPesquisar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
					//new Buscador().consultar(textFieldConsulta.getText(), textAreaResultados);
					//new Buscador().consultar(comboBoxConsulta.getSelectedItem().toString(), textAreaResultados, true);
				new Buscador().consultar(textFieldConsulta.getText(), textAreaResultados);
			}
		});
		
		JButton btnPesquisarTodos = new JButton("Pesquisar Todos");
		btnPesquisarTodos.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				try {
					new Buscador().consultarTodos(textAreaResultados);
				} catch (org.apache.lucene.queryparser.classic.ParseException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}				 
			}
		});
		
		JButton btnLimpar = new JButton("");
		btnLimpar.setSize(new Dimension(33, 14));
		btnLimpar.setIconTextGap(5);
		btnLimpar.setHorizontalTextPosition(SwingConstants.CENTER);
		btnLimpar.setMaximumSize(new Dimension(33, 14));
		btnLimpar.setMinimumSize(new Dimension(33, 14));
		btnLimpar.setPreferredSize(new Dimension(33, 14));
		btnLimpar.setIcon(new ImageIcon(TelaPrincipal.class.getResource("/javax/swing/plaf/metal/icons/ocean/paletteClose.gif")));
		btnLimpar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textAreaResultados.setText("");
			}
		});
		
		GroupLayout groupLayout = new GroupLayout(frmBuscador.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblConsulta)
							.addPreferredGap(ComponentPlacement.RELATED)
//							.addComponent(comboBoxConsulta, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE)
							.addGap(42)
							.addComponent(textFieldConsulta, GroupLayout.PREFERRED_SIZE, 386, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnPesquisar)
							.addComponent(btnPesquisarTodos)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnLimpar, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
							.addGap(29))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 706, Short.MAX_VALUE)
							.addGap(20))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblConsulta)
						.addComponent(textFieldConsulta, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnPesquisar)
						.addComponent(btnPesquisarTodos)
						.addComponent(btnLimpar, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
//						.addComponent(comboBoxConsulta, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		
		frmBuscador.getContentPane().setLayout(groupLayout);
		frmBuscador.setBounds(100, 100, 600, 400);
		frmBuscador.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}
